package com.schooleducation.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.schooleducation.entity.Lesson;
import com.schooleducation.entity.LessonContent;
import com.schooleducation.entity.LessonInfo;
import com.schooleducation.entity.LessonSubscribe;
import com.schooleducation.entity.User;
import com.schooleducation.service.ServiceFactory;

@Controller
@RequestMapping("/lessonInfo")
public class LessonInfoController {

	@Resource(name = "serviceFac")
	private ServiceFactory serviceFac;
	
	/**
	 * 获取页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value ="{id:\\+?[1-9][0-9]*$}", method = RequestMethod.GET)
	public String getPage(@PathVariable(value = "id") Integer id, HttpSession session, Model model) {
		User user = (User) session.getAttribute("user");
		Lesson l = this.serviceFac.getLessonService().getLessonById(id);
		if(l == null){
			return "error/noData";
		}
		LessonInfo li = this.serviceFac.getLessonService().getLessonInfoById(id);
		List<LessonContent> lcs = this.serviceFac.getLessonService().getLessonContentList(id);
		Long stuTotal = this.serviceFac.getUserService().getStudentCountByLesson(l);
		int state = 3;
		if(user != null) {
			LessonSubscribe ls = new LessonSubscribe();
			ls.setLesson(l);
			ls.setUser(user);
			ls = this.serviceFac.getLessonService().getSubscribeLessonByLessonAndUser(ls);
			if(ls != null) {
				state = ls.getState();
			}
			model.addAttribute("user", user);
		}
		
		model.addAttribute("lesson", l);
		model.addAttribute("lessonInfo", li);
		model.addAttribute("contentList", lcs);
		model.addAttribute("stuTotal", stuTotal);
		model.addAttribute("lessonState", state);
		
		return "lessonInfo";
	}
}
